﻿using HandyControl.Controls;
using SelfServiceWriteSystem;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace HandyControlProject5
{
    public partial class MainWindow : INotifyPropertyChanged
    {

        public MainList mainList;
        public SearchList searchList;
        public EventList eventList;
        public DocView docView;

        Timer timer = new Timer(1000);
        int clickCount = 0;
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            mainList = new MainList(this); 
            searchList = new SearchList(this);
            eventList = new EventList(this);
            docView = new DocView(this);

            UserContent = mainList;
            timer.Elapsed += Timer_Elapsed;
           

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();  //给BitmapImage对象赋予数据的时候，需要用BeginInit()开始，用EndInit()结束
            Uri uri = new Uri(System.Environment.CurrentDirectory + "\\MainIco.png", UriKind.Absolute);
            bitmapImage.UriSource = uri;
            bitmapImage.DecodePixelWidth = 320;   //对大图片，可以节省内存。尽可能不要同时设置DecodePixelWidth和DecodePixelHeight，否则宽高比可能改变
            bitmapImage.EndInit();

            mainIco.Source = bitmapImage;

            mainTitle.Text = File.ReadAllText("title.txt").Trim(); 
        }
        
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {            
            clickCount = 0;
            timer.Stop();
        }

        private UserControl _content;
        //内容呈现器绑定到UserContent
        public UserControl UserContent
        {
            get { return _content; }
            set
            {
                _content = value;
                OnPropertyChanged("UserContent");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(SearchKeyword.Text.Trim()))
            {
                MessageBox.Show("搜索关键词不能为空！");
                return;
            }
            searchList.Search(SearchKeyword.Text);
            UserContent = searchList;
        }

        private void Image_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            timer.Stop();
            timer.Start();
            if (++clickCount >= 4)
            {
                Topmost = false;
                PassWrods passWrods = new PassWrods();
                if (passWrods.ShowDialog() == true)
                {
                   
                    Close();
                }
                Topmost = true ;
            }
           
        }
    }
}
